﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QLKS
{
    class LayDuLieu
    {
        /// <summary>
        /// Lấy bảng dữ liệu (table) từ CSDL
        /// </summary>
        /// <param name="truyvan"></param>
        /// <returns></returns>
        public DataSet LayBangDuLieu(string truyvan)
        {
            KetNoi.ThietLapKetNoi();
            try
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(truyvan, KetNoi.Conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds;
            }
            catch { return null; }
        }

        /// <summary>
        /// Lấy trường dữ liệu (field) từ CSDL
        /// </summary>
        /// <param name="truyvan"></param>
        /// <returns></returns>
        public SqlDataReader LayTruongDuLieu(string truyvan)
        {
            KetNoi.ThietLapKetNoi();
            try
            {
                SqlCommand cmd = new SqlCommand(truyvan, KetNoi.Conn);
                SqlDataReader dr = cmd.ExecuteReader();
                return dr;
            }
            catch { return null; }
        }

        /// <summary>
        /// Ghi dữ liệu vào CSDL
        /// </summary>
        /// <param name="truyvan"></param>
        /// <returns></returns>
        public bool GhiDuLieu(string truyvan)
        {
            try
            {
                KetNoi.ThietLapKetNoi();
                SqlCommand cmd = new SqlCommand(truyvan, KetNoi.Conn);
                cmd.ExecuteNonQuery();
                KetNoi.NgatKetNoi();
                return true;
            }
            catch { return false; }
        }
    }
}
